//设计所获奖项等级下拉选框

//忽略VSCode命名报警问题
// ignore_for_file: camel_case_types, non_constant_identifier_names, unused_field, prefer_typing_uninitialized_variables, must_be_immutable

import 'package:flutter/material.dart';

class DropDownMenu_Award_level extends StatefulWidget {
  
  Function(String) onChanged;  // 设置回调函数用于全局性修改变量
  Function(String) onInit;   // 设置回调函数启动时给new_region_level赋全局变量

  DropDownMenu_Award_level({
    super.key,
    required this.onChanged,
    required this.onInit,
  });
  @override
  State<DropDownMenu_Award_level> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<DropDownMenu_Award_level> {
  
  final List<String> data = ["一等奖","二等奖","三等奖","其他"];
  late String _selectedAwardLevel;    // 专门用于存储下面被选中的变量,之所以要这么设置是防止对话框一结束就导致变量重置,所以搞一个页面生命周期内的变量防止这样的情况发生        
  // 覆写类的构造函数
  @override
  void initState ()
    {
      super.initState();
      setState(() {      // StatefulWidget里要赋初值的话必须用setState更新状态!
        _selectedAwardLevel=data.first;
      });
      widget.onInit(data.first);    // 执行初始化回调函数,传出data.first变量让默认选项变成data列表第一行
    }
    
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        DropdownButton<String>(
          value: _selectedAwardLevel,
          items: [
            DropdownMenuItem(
              value: '一等奖',
              child: SizedBox(
                child: Padding(
                  padding: const EdgeInsets.only(left: 20.0,right: 20.0,top:10,bottom: 10),
                  child: Text("一等奖"),
                ),
              ),
            ),
            DropdownMenuItem(
              value: '二等奖',
              child: Padding(
                padding: const EdgeInsets.only(left: 20.0,right: 20,top:10,bottom: 10),
                child: Text("二等奖"),
              ),
            ),
            DropdownMenuItem(
              value: '三等奖',
              child: Padding(
                padding: const EdgeInsets.only(left: 20.0,right: 20,top:10,bottom: 10),
                child: Text("三等奖"),
              ),
            ),
          ], 
          // 选项变化时更新选择的选项
          onChanged:(newValue){
            if (newValue!.isEmpty)    // 如果用户没有做选择直接选择默认选项
              {
                newValue=data.first;
              }
            setState(() {
              _selectedAwardLevel=newValue!;
            });
            widget.onChanged(newValue);   // 更新外部变量
          },
        ),
      ],
    );
  }
}

/*
DropdownMenu<String>(
          menuHeight: 200,
          initialSelection: data.first,
          onSelected: _onselect,
          dropdownMenuEntries:_buildMenuList(data),
          controller: widget.textcontroller_award_level,
        ),
*/